package org.colcon.web.model;

import java.util.HashMap;
import java.util.Map;

import org.colcon.web.model.framework.IGroup;
import org.colcon.web.model.framework.ILaw;
import org.colcon.web.model.framework.IProposal;
import org.colcon.web.model.framework.ITitle;
import org.colcon.web.model.framework.IVote;
import org.colcon.web.model.title.MemberTitle;
import org.colcon.web.model.title.RepresentativeTitle;

public class Group extends ParamItem implements IGroup
{
	private static final long serialVersionUID = 8534863965798154597L;
	
	public static final String
	
		// arguments
		TEXT = "text",
	
		// containers
		LAWS = "laws",
		TITLES = "titles",
		VOTES = "votes",
		PROPOSALS = "proposals",
		
		// params
		PUBLIC = "public",
		
		// titles
		MEMBER = "member",
		REPRESENTATIVE = "representative";

	private String text;
	
	public Group()
	{
		this(new HashMap<String, String>());
	}
	
	public Group(Map<String, String> args)
	{
		this(args.get(ID), args.get(TEXT));
	}
	
	public Group(String id, String text)
	{
		super(Model.getGroupContainerPath(), id);
		
		addContainer(LAWS);
		addContainer(TITLES);
		addContainer(VOTES);
		addContainer(PROPOSALS);
		
		ITitle memberTitle = new MemberTitle(getId());
		addTitle(memberTitle);
		
		ITitle congressMemberTitle = new RepresentativeTitle(getId());
		addTitle(congressMemberTitle);
		
		this.text = text;
	}
	
	public String getText()
	{
		return text;
	}
	
	public void setText(String text)
	{
		this.text = text;
	}
	
	public boolean isPublic()
	{
		return Boolean.valueOf(getParamValue(PUBLIC));
	}
	
	// MEMBERS
	
	public String[] getMembers()
	{
		return getMemberTitle().getHolders();
	}
	
	public boolean hasMember(String id)
	{
		return getMemberTitle().hasHolder(id);
	}
	
	public boolean addMember(String id)
	{
		return getMemberTitle().addHolder(id);
	}
	
	public boolean removeMember(String id)
	{
		return getMemberTitle().removeHolder(id);
	}
	
	// LAWS
	
	public ILaw[] getLaws()
	{
		return getGrandChildren(LAWS).toArray(new ILaw[0]);
	}
	
	public ILaw getLaw(String id)
	{
		return (ILaw) get(LAWS, id);
	}
	
	public boolean hasLaw(String id)
	{
		return has(LAWS, id);
	}
	
	public boolean addLaw(ILaw law)
	{
		return add(LAWS, law);
	}
	
	public boolean removeLaw(String id)
	{
		return remove(LAWS, id);
	}
	
	// TITLES
	
	public ITitle[] getTitles()
	{
		return getGrandChildren(TITLES).toArray(new ITitle[0]);
	}
	
	public ITitle getTitle(String id)
	{
		return (ITitle) get(TITLES, id);
	}
	
	public boolean hasTitle(String id)
	{
		return has(TITLES, id);
	}
	
	public boolean addTitle(ITitle title)
	{
		return add(TITLES, title);
	}
	
	public boolean removeTitle(String id)
	{
		return remove(TITLES, id);
	}
	
	public ITitle getMemberTitle()
	{
		return getTitle(MEMBER);
	}
	
	public ITitle getRepresentativeTitle()
	{
		return getTitle(REPRESENTATIVE);
	}
	
	// VOTES
	
	public IVote[] getVotes()
	{
		return (IVote[]) getGrandChildren(VOTES).toArray(new IVote[0]);
	}
	
	public IVote getVote(String id)
	{
		return (IVote) get(VOTES, id);
	}
	
	public boolean hasVote(String id)
	{
		return has(VOTES, id);
	}
	
	public boolean addVote(IVote vote)
	{
		return add(VOTES, vote);
	}
	
	public boolean removeVote(String id)
	{
		return remove(VOTES, id);
	}
	
	// PROPOSALS
	
	public IProposal[] getProposals()
	{
		return (IProposal[]) getGrandChildren(PROPOSALS).toArray(new IProposal[0]);
	}
	
	public IProposal getProposal(String id)
	{
		return (IProposal) get(PROPOSALS, id);
	}
	
	public boolean hasProposal(String id)
	{
		return has(PROPOSALS, id);
	}
	
	public boolean addProposal(IProposal proposal)
	{
		return add(PROPOSALS, proposal);
	}
	
	public boolean removeProposal(String id)
	{
		return remove(PROPOSALS, id);
	}
}
